package common

import (
	"net"
	"time"
)

func TcpGather(ip string, port string, timeout time.Duration) (string, float64) {
	address := net.JoinHostPort(ip, port)
	return TcpGather2(address, timeout)
}

func TcpGather2(address string, timeout time.Duration) (msg string, delayMs float64) {
	start := time.Now()
	conn, err := net.DialTimeout("tcp", address, timeout)
	end := time.Now()
	delayMicro := end.Sub(start).Microseconds()
	delayMs = float64(delayMicro) / 1000
	if err != nil {
		return "timeout", delayMs
	} else {
		_ = conn.Close()
		return "success", delayMs
	}
}
